package com.feige.weibo.util;

/**
 * Created by Administrator on 2016/11/10.
 */

import android.util.Log;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

public class HttpRequest {
    private static final String TAG = "HttpRequest";
    public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws IOException {

        /*
         * http://127.0.0.1/AndroidService/android/upload?title=aaa&timelength=90的形式
         */
        StringBuilder sb = new StringBuilder(path);
        sb.append('?');
        for(Map.Entry<String, String> entry : params.entrySet()) {
            sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');
        }
        sb.deleteCharAt(sb.length()-1);

        try {
            URL url = new URL(sb.toString());
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("GET");    //设置方法为GET
            conn.setReadTimeout(5 * 1000);   //设置过期时间为5秒
            if(conn.getResponseCode() == 200) {  //如果成功返回
                return true;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();

            Log.e(TAG, e.toString());
            return false;
        }
        return false;
    }
}